<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/19
 * Time: 10:11
 */
namespace App\Http\Controllers\Api\V1\Manager\Wx;


use App\Http\Controllers\Controller;
use App\Libs\httpUtil;
use App\Models\WxMenu;
use App\Services\Wx\AliService;
use Illuminate\Http\Request;


class MenuController extends Controller
{

    public function index()
    {
        $menu = WxMenu::select()->where(['parentId'=>0])->orderBy('menuId','asc')->get()->toArray();

        foreach ($menu as $k=>$v){

            $menu[$k]['menuSonList'] = WxMenu::select()->where(['parentId'=>$v['menuId']])->orderBy('menuId','asc')->get()->toArray();

        }
        return $this->apiResponse($menu);
    }


    public function show($menuId)
    {
        $menu = WxMenu::select()->where(['menuId'=>$menuId,'parentId'=>0])->first();
        if($menu['menuFlag']){
            $menu['menuSonList'] = WxMenu::select()->where(['parentId'=>$menuId])->get()->toArray();
        }
        return $this->apiResponse($menu);
    }

    public function store(Request $request)
    {
        $menu = $request->all();
        $menuId = $menu['menuId'];
        unset($menu['menuId']);
        if ($menuId){
            WxMenu::destroy($menuId);
            WxMenu::where('parentId',$menuId)->delete();
        }

        if ($menu['menuFlag'] == 1){
            $menuList = $menu['menuSonList'];
            unset($menu['menuSonList']);

            $menuId = WxMenu::insertGetId($menu);
            foreach ($menuList as $k=>$v){
                $v['parentId'] = $menuId;
                $v['menuFlag'] = 0;
                WxMenu::create($v);
            }
        }else{
            unset($menu['menuSonList']);
            WxMenu::create($menu);
        }
        return $this->apiResponse('');
    }

    public function destroy($menuId)
    {
        WxMenu::destroy($menuId);
        WxMenu::where('parentId',$menuId)->delete();
        return $this->apiResponse('');
    }

    public function publish()
    {
        $menu = WxMenu::select()->where('parentId',0)->orderBy('menuId','asc')->get()->toArray();
        $button = [];
        foreach ($menu as $key=>$value){
            if ($value['menuFlag'] == 1 ){
                $button[$key]['name'] = $value['menuName'];
                $sub_button= [];
                $sonList = WxMenu::select()->where('parentId',$value['menuId'])->orderBy('menuId','asc')->get()->toArray();
                foreach ($sonList as $k=>$v){

                    $sub_button[$k]['name'] = $v['menuName'];
                    if ($v['responseType'] == 1){
                        $sub_button[$k]['type'] = 'click';
                        $sub_button[$k]['key'] = "V".$k."_HELLO_WORLD";

                    }
                    if ($v['responseType'] == 2){
                        $sub_button[$k]['type'] = 'view';
                        $sub_button[$k]['url'] = $v['url'];

                    }
                    if ($v['responseType'] == 3){
                        $sub_button[$k]['type'] = 'miniprogram';
                        $sub_button[$k]['url'] = $v['url'];
                        $sub_button[$k]['appid'] = $v['appId'];
                        $sub_button[$k]['pagepath'] = $v['appPage'];

                    }
                    if ($v['responseType'] == 4){
                        $sub_button[$k]['type'] = 'media_id';
                        $sub_button[$k]['media_id'] = $v['mediaId'];

                    }

                }
                $button[$key]['sub_button'] = $sub_button;

            }else{
                $button[$key]['name'] = $value['menuName'];
                if ($value['responseType'] == 1){
                    $button[$key]['type'] = 'click';
                    $button[$key]['key'] = "V".$k."_HELLO_WORLD";

                }
                if ($value['responseType'] == 2){
                    $button[$key]['type'] = 'view';
                    $button[$key]['url'] = $value['url'];

                }
                if ($value['responseType'] == 3){
                    $button[$key]['type'] = 'miniprogram';
                    $button[$key]['url'] = $value['url'];
                    $button[$key]['appid'] = $value['appId'];
                    $button[$key]['pagepath'] = $value['appPage'];

                }
                if ($value['responseType'] == 4){
                    $button[$key]['type'] = 'media_id';
                    $button[$key]['media_id'] = $value['mediaId'];

                }
            }
        }
        $data['button'] = $button;
        $data = json_encode($data,JSON_UNESCAPED_UNICODE);
        $wx = new wxCommentController();
        $access_token = $wx->getAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;
        $httpUrl = new httpUtil();
        $res = $httpUrl->http_post($url, $data);
        $res = json_decode($res);
        return $this->apiResponse($res);
    }





}
